// searchHook.ts
import { useState, useCallback, useMemo } from "react";
import { useProducts } from "../store/useQueryStore/useQueryStore";
import { Product } from "@/business.InterfaceLayer/type";

export const useSearch = () => {
  const { products } = useProducts();
  const [searchQuery, setSearchQuery] = useState<string>("");

  const searchResults = useMemo(() => {
    return products.filter((product: Product) =>
      product.title.toLowerCase().includes(searchQuery.toLowerCase())
    );
  }, [products, searchQuery]);

  const handleSearchChange = useCallback((value: string) => {
    setSearchQuery(value);
  }, []);

  return { searchQuery, searchResults, handleSearchChange };
};
